package com.qf2207.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.graphbuilder.curve.MultiPath;
import com.qf2207.bo.DevicesBo;
import com.qf2207.common.LayuiPageVo;
import com.qf2207.common.R;
import com.qf2207.listener.WebDevicesListener;
import com.qf2207.pojo.Devices;
import com.qf2207.service.DevicesService;
import com.qf2207.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/*
 * @Author LX
 * @Date 2022/7/26    16:03
 * @Version 3.3
 */
@RestController
@RequestMapping("/devices")
public class DevicesController {
    @Autowired
    DevicesService devicesService;

    @RequestMapping("/devicesList")
    public LayuiPageVo<DevicesBo> devicePageData(@RequestParam Map<String,Object> params){
        System.out.println(params);
        return devicesService.devicesPageData(params);
    }

    @RequestMapping("/devicesadd")
    public R adddevices (Devices devices){
        System.out.println(devices);
        int result = devicesService.addDevices(devices);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @RequestMapping("/deletedevices")
    public R deletedevices (Integer id){
        int result = devicesService.deletedevices(id);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @RequestMapping("/devicesedit")
    public R devicesEdit (Devices devices){
        System.out.println(devices);
        int result = devicesService.devicesedit(devices);
        if(result == 1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @RequestMapping("/deletedevicesByIds")
    public R deleteDevicesByIds(String ids){
        int result = devicesService.deletedevicesByIds(ids);
        if(result>1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {
        if(file!=null){
            //得到文件名
            String originalFilename = file.getOriginalFilename();

            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile(inputStream, originalFilename);
            //把文件 URL 写给前端
            System.out.println("上传图片路径："+R.ok().data("url",url));
            return R.ok().data("url",url);
        }
        return R.ok();
    }



    @Autowired
    WebDevicesListener webpaperListener;
    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Devices devices, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("试卷信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, DevicesBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<DevicesBo> devicesBos = devicesService.devicesExcelData(devices);
        System.out.println(devicesBos);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(devicesBos);
    }
}
